﻿Imports System.Data.OleDb

Public Class Graphs
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try


            If (Not Page.IsPostBack) Then
                generateDropDownYear()
                pnlGraph.Visible = False
            End If
        Catch ex As Exception

        End Try




    End Sub

    Private Sub generateDropDownYear()
        Try

        
        Dim myvalues As ArrayList = New ArrayList()

        For index = 0 To 5
            myvalues.Add(DateTime.Now.Year - index)
        Next

        drpSelectYear.DataSource = myvalues
            drpSelectYear.DataBind()

        Catch ex As Exception

        End Try

    End Sub




    Private Sub generateBarGraph(ByVal jsonData As String)
        Try

            Dim data As String = ""
            data = data + " <script type =""text/javascript"">"
            data = data + "function getBarGraph() {"
            data = data + jsonData


            data = data + "var barChart = new $jit.BarChart({"
            data = data + "injectInto:  'infovis',"
            data = data + "animate: true,"
            data = data + "orientation:  'vertical',"
            data = data + "barsOffset: 20,"
            data = data + "Margin: {"
            data = data + "top: 5,"
            data = data + "left: 5,"
            data = data + "right: 5,"
            data = data + "bottom: 5"
            data = data + "},"
            data = data + " labelOffset: 5,"
            data = data + "type: useGradients ? 'stacked:gradient' : 'stacked',"
            data = data + "showAggregates: true,"
            data = data + "showLabels: true,"
            data = data + " Label: {"
            data = data + "type: labelType,"
            data = data + "size: 13,"
            data = data + "family:  'Arial',"
            data = data + "color:  'white'"
            data = data + "},"
            data = data + "Tips: {"
            data = data + "enable: true,"
            data = data + "onShow: function (tip, elem) {"
            data = data + "tip.innerHTML = ""<b>"" + elem.name + ""</b>: "" + elem.value;"
            data = data + "}"
            data = data + "}"
            data = data + "});"
            data = data + "barChart.loadJSON(json);"
            data = data + "var list = $jit.id('id-list'),"
            data = data + "button = $jit.id('update'),"
            data = data + "orn = $jit.id('switch-orientation');"
            data = data + "$jit.util.addEvent(button, 'click', function () {"
            data = data + "var util = $jit.util;"
            data = data + "if (util.hasClass(button, 'gray')) return;"
            data = data + "util.removeClass(button, 'white');"
            data = data + "util.addClass(button, 'gray');"
            data = data + "barChart.updateJSON(json2);"
            data = data + "});"
            data = data + "var legend = barChart.getLegend(),"
            data = data + "listItems = [];"
            data = data + "for (var name in legend) {"
            data = data + "listItems.push('<div class=\'query-color\' style=\'background-color:'"
            data = data + "+ legend[name] + '\'>&nbsp;</div>' + name);"
            data = data + "}"
            data = data + "list.innerHTML = '<li>' + listItems.join('</li><li>') + '</li>';"
            data = data + "}"
            data = data + "</script>"

            ltrGraphBar.Text = data

        Catch ex As Exception

        End Try
    End Sub






    Protected Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click

        Try


            If (drpSelectMonth.SelectedValue <> -1) And (drpSelectPage.SelectedValue <> -1) And (drpSelectYear.SelectedValue <> -1) Then

                Dim searchType As String = ""

                If (drpSelectPage.SelectedValue = 0) Then
                    searchType = "QuizOverview.aspx?quizId="
                ElseIf (drpSelectPage.SelectedValue = 1) Then
                    searchType = "PlayQuiz.aspx?quizId="
                ElseIf (drpSelectPage.SelectedValue = 2) Then
                    searchType = "ProfileViewer.aspx?ProfileId"
                End If
                Dim dr As OleDbDataReader
                If (drpSelectYear.SelectedValue = 0) And (drpSelectMonth.SelectedValue = 0) Then

                    Dim getSelectedType As User_PagesNameManagement = New User_PagesNameManagement()

                    dr = getSelectedType.SelectNumberOfVistorsForAllTimeByPageContent(searchType)

                ElseIf (drpSelectYear.SelectedValue <> 0) And (drpSelectMonth.SelectedValue <> 0) Then
                    Dim getSelectedType As User_PagesNameManagement = New User_PagesNameManagement()
                    dr = getSelectedType.SelectNumberOfVistorByPageContentByYearMonth(searchType, Convert.ToInt32(drpSelectYear.SelectedItem.ToString()), drpSelectMonth.SelectedValue)

                ElseIf (drpSelectYear.SelectedValue = 0) And (drpSelectMonth.SelectedValue <> 0) Then
                    Dim getSelectedType As User_PagesNameManagement = New User_PagesNameManagement()
                    dr = getSelectedType.SelectNumberOfVistorByPageContentByMonthAllYears(searchType, drpSelectMonth.SelectedValue)

                ElseIf (drpSelectYear.SelectedValue <> 0) And (drpSelectMonth.SelectedValue = 0) Then
                    Dim getSelectedType As User_PagesNameManagement = New User_PagesNameManagement()
                    dr = getSelectedType.SelectNumberOfVistorsByPageContentByYearMonthAll(searchType, Convert.ToInt32(drpSelectYear.SelectedItem.ToString()))
                End If


                If dr.HasRows Then
                    Dim Data As String = ""

                    Data = Data + "var json = {"
                    Data = Data + "'label': ['Visitors'],"
                    Data = Data + "'values': ["

                    pnlGraph.Visible = True

                    Do While dr.Read()
                        Dim pageName As String = dr("PageNameContent").ToString()
                        Dim graphContent As String = ""
                        If (pageName.Contains("QuizOverview")) Then
                            pageName = pageName.Replace("QuizOverview.aspx?quizId=", "")

                            Dim getQuiz As QuizManagement = New QuizManagement()
                            Dim dr1 As OleDbDataReader = getQuiz.SelectQuizByQuizId(Convert.ToInt32(pageName))

                            If dr1.HasRows Then
                                dr1.Read()
                                If Not IsDBNull(dr1("QuizName")) Then graphContent = dr1("QuizName").ToString()
                            End If



                        ElseIf (pageName.Contains("PlayQuiz")) Then
                            pageName = pageName.Replace("PlayQuiz.aspx?quizId=", "")

                            Dim getQuiz As QuizManagement = New QuizManagement()
                            Dim dr1 As OleDbDataReader = getQuiz.SelectQuizByQuizId(Convert.ToInt32(pageName))

                            If dr1.HasRows Then
                                dr1.Read()
                                If Not IsDBNull(dr1("QuizName")) Then graphContent = dr1("QuizName").ToString()
                            End If


                        ElseIf (pageName.Contains("ProfileViewer")) Then
                            pageName = pageName.Replace("ProfileViewer.aspx?ProfileId=", "")


                            Dim getProfile As ProfilesManagement = New ProfilesManagement()
                            Dim dr1 As OleDbDataReader = getProfile.SelectProfileByProfileID(Convert.ToInt32(pageName))

                            If dr1.HasRows Then
                                dr1.Read()
                                If Not IsDBNull(dr1("ProfileName")) Then graphContent = dr1("ProfileName").ToString()
                            End If


                        End If


                        Data = Data + "{"
                        Data = Data + "'label': '" + graphContent + "',"
                        Data = Data + "'values': [" + dr("visitors").ToString() + "]"
                        Data = Data + "},"


                    Loop


                    Data = Data.Substring(0, Data.Length - 1)
                    Data = Data + "]"
                    Data = Data + "};"

                    generateBarGraph(Data)

                    Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "getBarGraph();", True)
                    lblMessage.Text = ""
                Else
                    pnlGraph.Visible = False
                    lblMessage.Text = "No data was found"
                End If

            Else
                lblMessage.Text = "Select values from dropdown lists"
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class